package dao;

import entity.OrderAll;
import entity.OrderInfo;
import openConn.DruidUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrderInfoDao {
    public static List<OrderAll> selectOrderAll(int userID) throws SQLException {
        Connection connection = DruidUtils.openConn();
        String sql = "CALL select_all_order_by_user_id(?)";
        CallableStatement statement = connection.prepareCall(sql);
        statement.setInt(1,userID);
        ResultSet resultSet = statement.executeQuery();
        List<OrderAll> orderAllList = new ArrayList<>();
        while (resultSet.next()){
            int orderId = resultSet.getInt("order_id");
            String userName = resultSet.getString("user_name");
            String comName = resultSet.getString("com_name");
            Double comPrice = resultSet.getDouble("com_price");
            Double buyCount = resultSet.getDouble("buy_count");
            String province = resultSet.getString("province");
            String city = resultSet.getString("city");
            String area = resultSet.getString("area");
            String detail = resultSet.getString("detail");

            OrderAll orderAll = new OrderAll(orderId,userName,comName,comPrice,buyCount,province,city,area,detail);
            orderAllList.add(orderAll);
        }
        resultSet.close();
        statement.close();
        connection.close();
        return orderAllList;
    }

    public static List<OrderAll> selectOrderAll() throws SQLException {
        Connection connection = DruidUtils.openConn();
        String sql = "CALL select_all_order()";
        CallableStatement statement = connection.prepareCall(sql);
        ResultSet resultSet = statement.executeQuery();
        List<OrderAll> orderAllList = new ArrayList<>();
        while (resultSet.next()){
            int orderId = resultSet.getInt("order_id");
            String userName = resultSet.getString("user_name");
            String comName = resultSet.getString("com_name");
            Double comPrice = resultSet.getDouble("com_price");
            Double buyCount = resultSet.getDouble("buy_count");
            String province = resultSet.getString("province");
            String city = resultSet.getString("city");
            String area = resultSet.getString("area");
            String detail = resultSet.getString("detail");

            OrderAll orderAll = new OrderAll(orderId,userName,comName,comPrice,buyCount,province,city,area,detail);
            orderAllList.add(orderAll);
        }
        resultSet.close();
        statement.close();
        connection.close();
        return orderAllList;
    }

    public static int update (int orderId,int comId,int addressId,Double buyCount,Connection connection) throws SQLException {

        String sql = "update order_info set  com_id = ?, address_id =?,buy_count = ? where order_id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,comId);
        statement.setInt(2,addressId);
        statement.setDouble(3,buyCount);
        statement.setInt(4,orderId);
        int count = statement.executeUpdate();
        statement.close();
        return count;
    }

    public static OrderInfo selectById(int orderID,Connection connection) throws SQLException {
        String sql = "select * from order_info where order_id = ?";
        CallableStatement statement = connection.prepareCall(sql);
        statement.setInt(1,orderID);
        ResultSet resultSet = statement.executeQuery();
        List<OrderAll> orderAllList = new ArrayList<>();
        OrderInfo orderInfo = null;
        while (resultSet.next()){
            int orderId = resultSet.getInt("order_id");
            int userId = resultSet.getInt("user_id");
            int comId = resultSet.getInt("com_id");
            int addressId = resultSet.getInt("address_id");
            Double buyCount = resultSet.getDouble("buy_count");

            orderInfo = new OrderInfo(orderId,userId,comId,addressId,buyCount);

        }
        resultSet.close();
        statement.close();

        return orderInfo;
    }

    public static int delete(int orderId) throws SQLException {
        Connection connection = DruidUtils.openConn();
        String sql = "delete from order_info where order_id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,orderId);
        int count = statement.executeUpdate();
        System.out.println("删除成功");
        statement.close();
        connection.close();
        return count;
    }

}
